﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class AccumulationForm : Form
    {
        int id_client;
        string client_type;
        
        OracleConnection connection;
        public AccumulationForm(int idClient, string clientType)
        {
            id_client = idClient;
            client_type = clientType;
            InitializeComponent();
        }

        private void ClearList()
        {
            
        }

        public void RefreshTable()
        {
            //this.ClearList();
            AccumulationTypeClass selectMass = new AccumulationTypeClass();
            DBWorkClass dbWork = new DBWorkClass();
            selectMass = dbWork.SelectFromAccumulation(client_type, id_client);
            if (selectMass.number.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";
                dataGridView1[4, 0].Value = "";
                
            }
            else { dataGridView1.RowCount = selectMass.number.Count; }
            int i = 0;
            while (i < selectMass.number.Count)
            {
                dataGridView1[0, i].Value = selectMass.number[i].ToString();
                dataGridView1[1, i].Value = selectMass.proc[i].ToString();
                dataGridView1[2, i].Value = selectMass.start_date[i].ToString();
                dataGridView1[3, i].Value = selectMass.finish_date[i].ToString();
                if (Convert.ToInt32(selectMass.deposit[i]) != 0)
                { dataGridView1[4, i].Value = selectMass.deposit[i].ToString(); }
                else { dataGridView1[4, i].Value = "закрыт"; }
                i++;
            }
            if (selectMass.number.Count > 1) { buttonAdd.Enabled = false; }
            
        }

        private int GetId(int index)
        {                   
                AccumulationTypeClass selectMass = new AccumulationTypeClass();
                DBWorkClass dbWork = new DBWorkClass();
                selectMass = dbWork.SelectFromAccumulation(client_type, id_client);
                int id = Convert.ToInt32(selectMass.number[index]);                          
            return id;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddAccumulationForm addFr = new AddAccumulationForm(client_type,this,id_client);
            addFr.Show();
            
        }

        private void AccumulationForm_Load(object sender, EventArgs e)
        {
            this.RefreshTable();
            dataGridView1.ClearSelection();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int procent = Convert.ToInt32(dataGridView1[1, index].Value);
                int deposit = Convert.ToInt32(dataGridView1[4, index].Value);
                int numb = this.GetId(index);
                if (client_type.Equals("физический"))
                {
                    AccumulationOperationForm operationFr = new AccumulationOperationForm(client_type, this, id_client, deposit, procent, numb);
                    operationFr.Show();
                }
                else { EditAccumLegalClientForm Form = new EditAccumLegalClientForm(client_type, this, id_client, deposit, procent, numb); Form.Show(); }
            }
            catch (FormatException) { MessageBox.Show("фильтруй базар!"); }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int number = this.GetId(index);
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.DeleteFromAccumulation(number);
                this.RefreshTable();
            }
            catch (FormatException) { MessageBox.Show("фильтруй базар!"); }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            buttonDelete.Enabled = true;
            buttonEdit.Enabled = true;
        }
    }
}
